local M = {}

-- https://gitlab.com/jrop/dotfiles/-/blob/master/.config/nvim/lua/my/utils.lua#L13
function M.get_visual_selection()
  local a_orig = vim.fn.getreg('a')
  local mode = vim.fn.mode()
  if mode ~= 'v' and mode ~= 'V' then
    vim.cmd([[normal! gv]])
  end
  vim.cmd([[silent! normal! "aygv]])
  local text = vim.fn.getreg('a')
  vim.fn.setreg('a', a_orig)
  return text
end

function M.copy(v)
    vim.fn.setreg('"',v)
    vim.fn.setreg("1",v)
    vim.fn.setreg("+",v)
end

local escape_characters = '"\\/.*$^~[]'
-- double escaping is required due to interpretation by feedkeys and then search/replace
M.double_escape = function(str)
	return vim.fn.escape(vim.fn.escape(str, escape_characters), escape_characters)
end

-- k 中不能使用双引号，应改用单引号，例如:
-- local cmd = "!column -s'|' -o'|' -t"
M.send_key = function(k)
    vim.cmd(':call feedkeys("' .. k .. '")')
end

return M
-- EOP
